# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        self.flag = True
        self.search(root)
        return self.flag

    def search(self, note: TreeNode):
        if not note:
            return 0
        left = self.search(note.left) + 1
        right = self.search(note.right) + 1
        if (left - right) ** 2 > 1:
            self.flag = False
        return max(left, right)
